package dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import utilClasses.ListaRole;
import utilClasses.uRole;

public class daoRole {

	static void kreirajRolu(String naziv) throws SQLException
	{
		Connection c = daoKonekcija.dajKonekciju();
		CallableStatement cStmt = c.prepareCall("{CALL Insert_Role(?)}");
		cStmt.setString(1, naziv);
		cStmt.execute();
		cStmt.close();
		c.close();
	}

	static int dajidRole(String naziv) throws SQLException
	{
		Connection c = daoKonekcija.dajKonekciju();
		PreparedStatement ps=c.prepareStatement("SELECT idRole FROM role WHERE nazivRole=?");
		ps.setString(1, naziv);
		ResultSet rs = ps.executeQuery();
		if(!rs.next()) return -1;
		int rez=rs.getInt(1);
		rs.close();
		ps.close();
		c.close();
		return rez;
		
	}

	public static String insertRola(String naziv,String korisnik) throws SQLException
	{
		Connection c = daoKonekcija.dajKonekciju();
		try {
			Integer idRole=dajidRole(naziv);
			if(idRole==-1)
			{
				kreirajRolu(naziv);
			}
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.print(e.getMessage());
			return new Boolean(false).toString();
		}
		return new Boolean(true).toString();
	}

	public static ListaRole dajRole(String user) throws SQLException
	{
		Connection c = daoKonekcija.dajKonekciju();
		ListaRole rez=new ListaRole();
		try {
			CallableStatement cStmt = c.prepareCall("{CALL dajRoleKorisnika(?)}"); 
			cStmt.setString(1,user); 
			
			ResultSet rs = cStmt.executeQuery();
			while (rs.next()) {
		    	String rola=rs.getString(1);
		    	String jelIma=rs.getString(2);
		    	System.out.print(rola+" "+jelIma);
		        rez.getRoles().add(new uRole(rola,jelIma)); 
		        
		    
		        }
			rs.close();
			cStmt.close();
			c.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			if(c!=null)c.close();
			e.printStackTrace();
		}
		
		return rez;
	}

	public static ListaRole dajSamoRoleKojeImam(String user) throws SQLException
	{
		Connection c = daoKonekcija.dajKonekciju();
		ListaRole rez=new ListaRole();
		try {
			CallableStatement cStmt = c.prepareCall("{CALL RolaKorisnik(?)}");
			cStmt.setString(1,user); 
			ResultSet rs = cStmt.executeQuery(); ;
			while (rs.next()) {
				String rola=rs.getString(1);
				 	System.out.print(rola);
			    rez.getRoles().add(new uRole(rola,"true"));      
			
			    }
			rs.close();
			cStmt.close();
			c.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			if(c!=null)c.close();
			e.printStackTrace();
		}
		return rez;
	}

	public static int brojRola(String user) throws SQLException{
		Connection c = daoKonekcija.dajKonekciju();
		try {
			CallableStatement cStmt = c.prepareCall("{CALL BrojRola(?)}"); 
			cStmt.setString(1,user); 
			ResultSet rs=cStmt.executeQuery(); 
			System.out.println("Uspjesno"); 
			
			if(rs.next()){
				int brrola=rs.getInt(1);
				return brrola;
			}
			rs.close();
			cStmt.close();
			c.close();
			return 0;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			if(c!=null)c.close();
			e.printStackTrace();
			return -1;
		} 
	}

	static void provjeraRole(String imePrezime) throws SQLException { 
		Connection c = daoKonekcija.dajKonekciju();
		CallableStatement cStmt = c.prepareCall("{CALL RolaKorisnik(?)}"); 
	cStmt.setString(1,imePrezime); 
	cStmt.execute(); 
	System.out.println("Uspjesno"); 
	cStmt.close(); 
	c.close();
	}

	static int dajUserRola(String naziv,String korisnik) throws SQLException
	{
		Connection c = daoKonekcija.dajKonekciju();
		PreparedStatement ps = c.prepareStatement("SELECT * from userrole as ur, role as r WHERE ur.rolaID=r.idrole AND ur.userID=? AND r.nazivRole=?");
		ps.setString(1, korisnik);
		ps.setString(2, naziv);
		ResultSet rs = ps.executeQuery();
		int tmp=-1;
		if(rs.next())  tmp=1;
		rs.close();
		ps.close();
		c.close();
		return tmp;
	}

	static void brisiUserRola(String korisnik,String naziv) throws Exception
	{
		Connection c = daoKonekcija.dajKonekciju();
		int idRole=dajidRole(naziv);
		if(idRole<0) throw new Exception("Nema role sa tim ID-em!");
		CallableStatement cStmt = c.prepareCall("{CALL deleteUserRole(?,?)}"); 
		cStmt.setString(2,korisnik);
		cStmt.setInt(1,idRole);
		cStmt.execute(); 
		System.out.println("Uspjesno"); 
		cStmt.close();
		c.close();
	}

	public static String updateUserRola(String naziv,String korisnik, String nebrisem) throws SQLException
	{
		Connection c = daoKonekcija.dajKonekciju();
		try {
			int imaLiVeze=dajUserRola(naziv,korisnik);
			if(imaLiVeze<0&&nebrisem.equals("true")) {
				System.out.print("Kreiram");
				daoRole.kreirajUserRola(korisnik,naziv);}
			if(imaLiVeze>0&&nebrisem.equals("false")) {
				brisiUserRola(korisnik,naziv);
				System.out.print("Brisem");
				}
			System.out.print(Integer.toString(imaLiVeze)+nebrisem);
			return new String("true");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new String("false");
		} catch (Exception e) {
			
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new String("false");
		}
	}

	static void kreirajUserRola(String korisnik,String naziv) throws Exception
	{
		Connection c = daoKonekcija.dajKonekciju();
		CallableStatement cStmt = c.prepareCall("{CALL Insert_UserRole(?,?)}"); 
		cStmt.setString(1,korisnik);
		int idRole=dajidRole(naziv);
		if(idRole<0) throw new Exception("Nema role sa tim ID-em!");
		cStmt.setInt(2,idRole);
		cStmt.execute(); 
		System.out.println("Uspjesno"); 
		cStmt.close();
		c.close();
	}

}